home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1993 November / JCSM Shareware Collection - 1993-11.iso / cl720 / sst115j.lzh / SSTEDT.C < prev    next >
C/C++ Source or Header  |  1992-07-18  |  21KB  |  803 lines

  1. /* ------------------------------------------------------------------------ */
  2. /*                                sstedit.c                                 */
  3. /*                         high-level editor routines                       */
  4. /*                                                                          */
  5. /*      CopyRight (C) 1991,1992  Steven Lutrov.   All rights reserved.      */
  6. /* ------------------------------------------------------------------------ */
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <mem.h>
  10. #include <conio.h>
  11. #include <alloc.h>
  12.  
  13. #include "sstvid.h"
  14. #include "sstwin.h"
  15. #include "sstkey.h"
  16.  
  17.  
  18. /* ------------------------------------------------------------------------ */
  19. /*                                macros                                    */
  20. /* ------------------------------------------------------------------------ */
  21. #define NEXTTAB     (TAB-(x%TAB))
  22. #define LASTTAB     ((wwd/TAB)*TAB)
  23. #define PREVTAB     (((x-1)%TAB)+1)
  24. #define curr(x,y)   (bfptr+(y)*wwd+(x))
  25. #define lineno(y)   ((int)(bfptr-topptr)/wwd+(y))
  26.  
  27. /* ------------------------------------------------------------------------ */
  28. /*                            local variables                               */
  29. /* ------------------------------------------------------------------------ */
  30. extern int VSG;
  31. int last_x, last_y;
  32. static int wht;
  33. static int wwd;
  34. static int wsz;
  35. static char *topptr;
  36. static char *bfptr;
  37. static char *lstptr;
  38. static int lines;
  39. static char *endptr;
  40. static int blkbeg;
  41. static int blkend;
  42. static int inserting;
  43. static WINDOW *wnd;
  44. static int do_display_text = 1;
  45.  
  46.  
  47. /* ------------------------------------------------------------------------ */
  48. /*                        local function prototypes                         */
  49. /* ------------------------------------------------------------------------ */
  50. void  erase_buffer    (int *x, int *y);
  51. int   lastword        (int x, int y);
  52. void  last_char       (int *x, int *y);
  53. void  test_para       (int x, int y);
  54. int   trailing_spaces (int y);
  55. int   first_wordlen   (int y);
  56. void  paraform        (int x, int y);
  57. int   blankline       (int line);
  58. void  delete_word     (int x, int y);
  59. void  delete_line     (int y);
  60. void  delete_block    (void);
  61. void  copy_block      (int y);
  62. void  move_block      (int y);
  63. void  mvblock         (int y, int moving);
  64. void  findlast        (void);
  65. void  find_end        (int *x, int *y);
  66. void  carrtn          (int *x, int *y, int insert);
  67. void  backspace       (int *x, int *y);
  68. void  fore_word       (int *x, int *y, char *bf);
  69. int   spaceup         (int *x, int *y, char **bf);
  70. void  back_word       (int *x, int *y, char *bf);
  71. int   spacedn         (int *x, int *y, char **bf);
  72. void  forward         (int *x, int *y);
  73. int   downward        (int *y);
  74. void  upward          (int *y);
  75. void  display_text    (void);
  76. void  disp_line       (int y);
  77. void  insert_line     (void);
  78.  
  79.  
  80. /* ------------------------------------------------------------------------ */
  81. /*                      Process text entry for a window.                    */
  82. /* ------------------------------------------------------------------------ */
  83. void Eopen(WINDOW *wnd1, char *bf, int bsize)
  84. {
  85.     char *b, *buff;
  86.     int depart = FALSE, i, c;
  87.     int x, y, svx, svlw, tx, tabctr = 0;
  88.  
  89.     wnd = wnd1;
  90.     wht = HEIGHT-2;
  91.     wwd = WIDTH-2;
  92.     wsz = wwd * wht;
  93.     topptr = bfptr = bf;
  94.     lines = bsize / wwd;
  95.     endptr = bf + wwd * lines;
  96.     blkbeg = 0;
  97.     blkend = 0;
  98.     inserting = FALSE;
  99.     x = 0;
  100.     y = 0;
  101.     display_text();
  102.     /* --------- read in text from the keyboard ---------- */
  103.     findlast();
  104.     while (TRUE)    {
  105.         last_x = COL + 1 + x;
  106.         last_y = ROW + 1 + y;
  107.         vsetcur(last_x, last_y);
  108.         buff = curr(x, y);
  109.         if (tabctr)    {
  110.             --tabctr;
  111.             c = ' ';
  112.         }
  113.         else    {
  114.             c = kgetch();
  115.             /* Wemsgclear();  */
  116.         }
  117.         switch (c)    {
  118.             case '\r':    carrtn(&x, &y, inserting);
  119.                         break;
  120.             case DOWN:    downward(&y);
  121.                         break;
  122.             case PGUP:    y = 0;
  123.                         for (i = 0; i < wht; i++)
  124.                             upward(&y);
  125.                         break;
  126.             case PGDN:    y = HEIGHT - 2;
  127.                         for (i = 0; i < wht; i++)
  128.                             downward(&y);
  129.                         y = 0;
  130.                         break;
  131.             case '\t':    if (x + NEXTTAB < wwd)    {
  132.                             if (inserting)
  133.                                 tabctr = NEXTTAB;
  134.                             else
  135.                                 x += NEXTTAB;
  136.                         }
  137.                         else
  138.                             carrtn(&x, &y, inserting);
  139.                         break;
  140.             case SHIFT_TAB:
  141.                         if (x < TAB)    {
  142.                             upward(&y);
  143.                             x = LASTTAB;
  144.                         }
  145.                         else
  146.                             x -= PREVTAB;
  147.                         break;
  148.             case CTRL_RIGHT:
  149.                         fore_word(&x, &y, buff);
  150.                         break;
  151.             case CTRL_LEFT:
  152.                         back_word(&x, &y, buff);
  153.                         break;
  154.             case CTRL_PGUP:
  155.                         y = wht - 1;
  156.                         break;
  157.             case CTRL_PGDN:
  158.                         y = 0;
  159.                         break;
  160.             case CTRL_HOME:
  161.                         x = y = 0;
  162.                         bfptr = topptr;
  163.                         display_text();
  164.                         break;
  165.             case HOME:    x = 0;
  166.                         break;
  167.             case CTRL_END:
  168.                         find_end(&x, &y);
  169.                         display_text();
  170.                         break;
  171.             case END:    last_char(&x, &y);
  172.                         break;
  173.             case UP:    upward(&y);
  174.                         break;
  175.             case F2:
  176.             case ESC:    depart = TRUE;
  177.                         break;
  178.             case '\b':
  179.             case LEFT:    if (!(x || y))
  180.                             break;
  181.                         backspace(&x, &y);
  182.                         if (x == wwd - 1)
  183.                             last_char(&x, &y);
  184.                         if (c == LEFT)
  185.                             break;
  186.                         buff = curr(x, y);
  187.             case DEL:    movmem(buff+1, buff, wwd-1-x);
  188.                         *(buff+wwd-1-x) = ' ';
  189.                         disp_line(y);
  190.                         test_para(x+1, y);
  191.                         break;
  192.             case CTRL_Y:    delete_line(y);
  193.                         break;
  194.             case CTRL_D:delete_word(x, y);
  195.                         test_para(x, y);
  196.                         break;
  197.             case INS:    inserting ^= TRUE;
  198.                         insert_line();
  199.                         break;
  200.             case F3:    erase_buffer(&x, &y);
  201.                         break;
  202.             case F4:    paraform(0, y);
  203.                         break;
  204.             case F7:    blkbeg = lineno(y) + 1;
  205.                         if (blkbeg > blkend)
  206.                             blkend = lines;
  207.                         display_text();
  208.                         break;
  209.             case F8:    blkend = lineno(y) + 1;
  210.                         if (blkend < blkbeg)
  211.                             blkbeg = 1;
  212.                         display_text();
  213.                         break;
  214.             case ALT_F8:    move_block(y);
  215.                         break;
  216.             case ALT_F7:    copy_block(y);
  217.                         break;
  218.             case F9:    delete_block();
  219.                         break;
  220.             case F10:    blkbeg = blkend = 0;
  221.                         display_text();
  222.                         break;
  223.             case RIGHT:    forward(&x, &y);
  224.                         break;
  225.             default:    if (!isprint(c))
  226.                             break;
  227.                         if (curr(x, y) == endptr-1 ||
  228.                            (lineno(y)+1 >= lines && inserting
  229.                                 && *curr(wwd-2, y) != ' '))    {
  230.                             /* Wemsg(" End of Buffer "); */
  231.                             break;
  232.                         }
  233.                         if (inserting)    {
  234.                             buff = curr(x, y);
  235.                             movmem(buff, buff + 1, wwd-1-x);
  236.                         }
  237.                         buff = curr(x, y);
  238.                         if (buff < endptr)    {
  239.                             if (buff >= lstptr)
  240.                                 lstptr = buff + 1;
  241.                             *buff = c;
  242.                             disp_line(y);
  243.                         }
  244.                         buff = curr(wwd-1, y);
  245.                         if (endptr && *buff != ' ')    {
  246.                             for (b = buff+1; b < endptr; b++)
  247.                                 if (*b==' ' && *(b + 1)==' ')
  248.                                     break;
  249.                             movmem(buff+1, buff+2, b-buff-1);
  250.                             *(buff+1) = ' ';
  251.                             svx = x;
  252.                             svlw = lastword(x, y);
  253.                             x = wwd-1;
  254.                             if (*(buff-1) != ' ')
  255.                                 back_word(&x, &y, buff);
  256.                             tx = x;
  257.                             carrtn(&x, &y, TRUE);
  258.                             if (svlw)
  259.                                 x = svx-tx;
  260.                             else    {
  261.                                 x = svx;
  262.                                 --y;
  263.                             }
  264.                         }
  265.                         forward(&x, &y);
  266.                         break;
  267.         }
  268.         if (depart)
  269.             break;
  270.     }
  271.     inserting = FALSE;
  272.     insert_line();
  273. }
  274. /* ------------------------------------------------------------------------ */
  275. /*                           erase the buffer                               */
  276. /* ------------------------------------------------------------------------ */
  277. static void erase_buffer(int *x, int *y)
  278. {
  279.     int c = 0;
  280.     WINDOW *sur;
  281.  
  282.     sur = Westablish(28, 11, 4, 24);
  283.     Wsetcolour(sur, WIN_ALL, RED, YELLOW, BRIGHT);
  284.     Wshow(sur);
  285.     Wprintf(sur, " Erase text window\n Are you sure? (y/n)");
  286.     while (c != 'y' && c != 'n')    {
  287.         c = kgetch();
  288.         c = tolower(c);
  289.         if (c == 'y')    {
  290.             lstptr = bfptr = topptr;
  291.             *x = *y = 0;
  292.             setmem(bfptr, lines * wwd, ' ');
  293.             blkbeg = blkend = 0;
  294.             display_text();
  295.         }
  296.     }
  297.     Wdelete(sur);
  298. }
  299.  
  300. /* ------------------------------------------------------------------------ */
  301. /*                see if a word is the last word on the line                */
  302. /* ------------------------------------------------------------------------ */
  303. static int lastword(int x, int y)
  304. {
  305.     char *bf = curr(x, y);
  306.  
  307.     while (x++ < wwd-1)
  308.         if (*bf++ == ' ')
  309.             return 0;
  310.     return 1;
  311. }
  312.  
  313. /* ------------------------------------------------------------------------ */
  314. /*                 go to last displayable character on the line             */
  315. /* ------------------------------------------------------------------------ */
  316. static void last_char(int *x, int *y)
  317. {
  318.     char *bf;
  319.  
  320.     *x = wwd-1;
  321.     bf = curr(0, *y);
  322.     while (*x && *(bf + *x) == ' ')
  323.         --(*x);
  324.     if (*x && *x < wwd - 1)
  325.         (*x)++;
  326. }
  327.  
  328. /* ------------------------------------------------------------------------ */
  329. /*                 test to see if paragraph should be reformed              */
  330. /* ------------------------------------------------------------------------ */
  331. static void test_para(int x, int y)
  332. {
  333.     int ts, fw;
  334.     int far *c;
  335.  
  336.     c = kstate();
  337.     if (! (*c & K_SCROLLLOCKON) && y < lines)    {
  338.         ts = trailing_spaces(y);
  339.         fw = first_wordlen(y+1);
  340.         if (fw && ts > fw)
  341.             paraform(x, y);
  342.     }
  343. }
  344.  
  345. /* ------------------------------------------------------------------------ */
  346. /*                   count the trailing spaces on a line                    */
  347. /* ------------------------------------------------------------------------ */
  348. static int trailing_spaces(int y)
  349. {
  350.     int x = wwd-1, ct = 0;
  351.     char *bf = curr(0, y);
  352.  
  353.     while (x >= 0)    {
  354.         if (*(bf + x) != ' ')
  355.             break;
  356.         --x;
  357.         ct++;
  358.     }
  359.     return ct;
  360. }
  361. /* ------------------------------------------------------------------------ */
  362. /*              count the length of the first word on a line                */
  363. /* ------------------------------------------------------------------------ */
  364. static int first_wordlen(int y)
  365. {
  366.     int ct = 0, x = 0;
  367.     char *bf = curr(0, y);
  368.  
  369.     while (x < wwd-1 && *(bf+x) == ' ')
  370.         x++;
  371.     while (x+ct < wwd-1 && *(bf+x+ct) != ' ')
  372.         ct++;
  373.     return ct;
  374. }
  375.  
  376.  
  377. /* ------------------------------------------------------------------------ */
  378. /*                             form a paragraph                             */
  379. /* ------------------------------------------------------------------------ */
  380. static void paraform(int x, int y)
  381. {
  382.     char *cp1, *cp2, *cpend, *svcp;
  383.     int x1;
  384.  
  385.     if (blankline(lineno(y)+1))
  386.         return;
  387.     if (!blkbeg)    {
  388.         blkbeg = blkend = lineno(y)+1;
  389.         blkend++;
  390.         while (blkend < lines)    {
  391.             if (blankline(blkend))
  392.                 break;
  393.             blkend++;
  394.         }
  395.         --blkend;
  396.     }
  397.     if (lineno(y) != blkbeg-1)
  398.         x = 0;
  399.     x1 = x;
  400.     cp1 = cp2 = topptr + (blkbeg - 1) * wwd + x;
  401.     cpend = topptr + blkend * wwd;
  402.     while (cp2 < cpend)    {
  403.         while (*cp2 == ' ' && cp2 < cpend)
  404.             cp2++;
  405.         if (cp2 == cpend)
  406.             break;
  407.         /* at a word */
  408.         while (*cp2 != ' ' && cp2 < cpend)    {
  409.             if (x1 >= wwd - 1)    {
  410.                 /* wrap the word */
  411.                 svcp = cp1 + (wwd - x1);
  412.                 while (*--cp1 != ' ')    {
  413.                     *cp1 = ' ';
  414.                     --cp2;
  415.                 }
  416.                 x1 = 0;
  417.                 blkbeg++;
  418.                 cp1 = svcp;
  419.             }
  420.             *cp1++ = *cp2++;
  421.             x1++;
  422.         }
  423.         if (cp2 < cpend)    {
  424.             *cp1++ = ' ';
  425.             x1++;
  426.         }
  427.     }
  428.     while (cp1 < cpend)
  429.         *cp1++ = ' ';
  430.     blkbeg++;
  431.     if (blkbeg <= blkend)
  432.         delete_block();
  433.     blkbeg = blkend = 0;
  434.     display_text();
  435.     findlast();
  436. }
  437.  
  438. /* ------------------------------------------------------------------------ */
  439. /*                            test for a blank line                         */
  440. /* ------------------------------------------------------------------------ */
  441. static int blankline(int line)
  442. {
  443.     char *cp;
  444.     int x;
  445.  
  446.     cp = topptr + (line-1) * wwd;
  447.     for (x = 0; x < wwd; x++)
  448.         if (*(cp + x) != ' ')
  449.             break;
  450.     return (x == wwd);
  451. }
  452.  
  453. /* ------------------------------------------------------------------------ */
  454. /*                             delete a word                                */
  455. /* ------------------------------------------------------------------------ */
  456. static void delete_word(int x, int y)
  457. {
  458.     int wct = 0;
  459.     char *cp1, *cp2;
  460.  
  461.     cp1 = cp2 = curr(x, y);
  462.     if (*cp2 == ' ')
  463.         while (*cp2 == ' ' && x + wct < wwd)    {
  464.             wct++;
  465.             cp2++;
  466.         }
  467.     else    {
  468.         while (*cp2 != ' ' && x + wct < wwd)    {
  469.             wct++;
  470.             cp2++;
  471.         }
  472.         while (*cp2 == ' ' && x + wct < wwd)    {
  473.             wct++;
  474.             cp2++;
  475.         }
  476.     }
  477.     movmem(cp2, cp1, wwd - x - wct);
  478.     setmem(cp1 + wwd - x - wct, wct, ' ');
  479.     display_text();
  480.     findlast();
  481. }
  482.  
  483. /* ------------------------------------------------------------------------ */
  484. /*                              delete a line                               */
  485. /* ------------------------------------------------------------------------ */
  486. static void delete_line(int y)
  487. {
  488.     char *cp1, *cp2;
  489.     int len;
  490.  
  491.     cp1 = bfptr + y * wwd;
  492.     cp2 = cp1 + wwd;
  493.     if (cp1 < lstptr)    {
  494.         len = endptr - cp2;
  495.         movmem(cp2, cp1, len);
  496.         lstptr -= wwd;
  497.         setmem(endptr - wwd, wwd, ' ');
  498.         display_text();
  499.     }
  500. }
  501.  
  502. /* ------------------------------------------------------------------------ */
  503. /*                             delete a block                               */
  504. /* ------------------------------------------------------------------------ */
  505. static void delete_block()
  506. {
  507.     char *cp1, *cp2;
  508.     int len;
  509.  
  510.     if (!blkbeg || !blkend)    {
  511.         putchar(7);
  512.         return;
  513.     }
  514.     cp1 = topptr + blkend * wwd;
  515.     cp2 = topptr + (blkbeg - 1) * wwd;
  516.     len = endptr - cp1;
  517.     movmem(cp1, cp2, len);
  518.     setmem(cp2 + len, endptr - (cp2 + len), ' ');
  519.     blkbeg = blkend = 0;
  520.     lstptr -= (cp1 - cp2);
  521.     display_text();
  522. }
  523.  
  524. /* ------------------------------------------------------------------------ */
  525. /*                       move and copy text blocks                          */
  526. /* ------------------------------------------------------------------------ */
  527. static void mvblock(int y, int moving)
  528. {
  529.     char *cp1, *cp2, *hd;
  530.     int len;
  531.     if (!blkbeg || !blkend)    {
  532.         vbeep(ERROR);
  533.         return;
  534.     }
  535.     if (lineno(y) >= blkbeg-1 && lineno(y) <= blkend-1)    {
  536.         /* Wemsg("Can't move/copy a block into itself"); */
  537.         return;
  538.     }
  539.     len = (blkend - blkbeg + 1) * wwd;
  540.     if ((hd = malloc(len)) == 0)
  541.         return;
  542.     cp1 = topptr + (blkbeg-1) * wwd;
  543.     movmem(cp1, hd, len);
  544.     cp2 = topptr + lineno(y) * wwd;
  545.     if (moving)    {
  546.         if (lineno(y) > blkbeg-1)
  547.             cp2 -= len;
  548.         do_display_text = 0;
  549.         delete_block();
  550.         do_display_text = 1;
  551.     }
  552.     if (cp2+len <= endptr)    {
  553.         movmem(cp2, cp2 + len, endptr - cp2 - len);
  554.         movmem(hd, cp2, len);
  555.     }
  556.     free(hd);
  557.     blkbeg = blkend = 0;
  558.     display_text();
  559. }
  560. /* ------------------------------------------------------------------------ */
  561. /*                              copy a block                                */
  562. /* ------------------------------------------------------------------------ */
  563. static void copy_block(int y)
  564. {
  565.     mvblock(y, FALSE);
  566.     findlast();
  567. }
  568.  
  569. /* ------------------------------------------------------------------------ */
  570. /*                             move a block                                 */
  571. /* ------------------------------------------------------------------------ */
  572. static void move_block(int y)
  573. {
  574.     mvblock(y, TRUE);
  575. }
  576.  
  577. /* ------------------------------------------------------------------------ */
  578. /*                  find the last character in the buffer                   */
  579. /* ------------------------------------------------------------------------ */
  580. static void findlast()
  581. {
  582.     register char *lp = endptr - 1;
  583.     register char *tp = topptr;
  584.  
  585.     while (lp > tp && (*lp == ' ' || *lp == '\0'))    {
  586.         if (*lp == '\0')
  587.             *lp = ' ';
  588.         --lp;
  589.     }
  590.     if (*lp != ' ')
  591.         lp++;
  592.     lstptr = lp;
  593. }
  594.  
  595. /* ------------------------------------------------------------------------ */
  596. /*                 go to the end of the data in the buffer                  */
  597. /* ------------------------------------------------------------------------ */
  598. static void find_end(int *x, int *y)
  599. {
  600.     int ct;
  601.  
  602.     bfptr = lstptr;
  603.     ct = (lstptr - topptr) % wsz;
  604.     bfptr -= ct;
  605.     if (bfptr + wsz > endptr)
  606.         bfptr = endptr - wsz;
  607.     *y = (ct / wwd);
  608.     *x = 0;
  609.     downward(y);
  610. }
  611.  
  612. /* ------------------------------------------------------------------------ */
  613. /*                              carriage return                             */
  614. /* ------------------------------------------------------------------------ */
  615. static void carrtn(int *x, int *y, int insert)
  616. {
  617.     int insct;
  618.     char *cp, *nl;
  619.     int ctl = 2;
  620.  
  621.     cp = curr(*x, *y);
  622.     nl = cp + ((cp - topptr) % wwd);
  623.     if (lineno(*y) + 2 < lines)
  624.         if (insert && nl < endptr)    {
  625.             insct = wwd - *x;
  626.             while (ctl--)    {
  627.                 if (endptr > cp + insct)    {
  628.                     movmem(cp, cp+insct, endptr-insct-cp);
  629.                     setmem(cp, insct, ' ');
  630.                 }
  631.                 else if (ctl == 1)
  632.                     setmem(cp, endptr - cp, ' ');
  633.                 cp += insct * 2;
  634.                 insct = *x;
  635.             }
  636.         }
  637.     *x = 0;
  638.     downward(y);
  639.     if (insert)    {
  640.         test_para(*x, *y);
  641.         display_text();
  642.     }
  643.     if (lineno(*y) + 2 < lines)
  644.         if (insert)
  645.             if ((lstptr + wwd) <= endptr)
  646.                 if (lstptr > curr(*x, *y))
  647.                     lstptr += wwd;
  648. }
  649.  
  650.  
  651. /* ------------------------------------------------------------------------ */
  652. /*                move the buffer offset back one position                  */
  653. /* ------------------------------------------------------------------------ */
  654. static void backspace(int *x, int *y)
  655. {
  656.     if (*x == 0)    {
  657.         if (*y)
  658.             *x = wwd - 1;
  659.         upward(y);
  660.     }
  661.     else
  662.         --(*x);
  663. }
  664.  
  665. /* ------------------------------------------------------------------------ */
  666. /*                  move the buffer offset forward one word                 */
  667. /* ------------------------------------------------------------------------ */
  668. static void fore_word(int *x, int *y, char *bf)
  669. {
  670.     while (*bf != ' ')    {
  671.         if (spaceup(x, y, &bf) == 0)
  672.             return;
  673.         if (*x == 0)
  674.             break;
  675.     }
  676.     while (*bf == ' ')
  677.         if (spaceup(x, y, &bf) == 0)
  678.             return;
  679. }
  680.  
  681. /* ------------------------------------------------------------------------ */
  682. static int spaceup(int *x, int *y, char **bf)
  683. {
  684.     if (*bf == lstptr)
  685.         return 0;
  686.     (*bf)++;
  687.     forward(x, y);
  688.     return 1;
  689. }
  690.  
  691. /* ------------------------------------------------------------------------ */
  692. /*                 move the buffer offset backward one word                 */
  693. /* ------------------------------------------------------------------------ */
  694. static void back_word(int *x, int *y, char *bf)
  695. {
  696.     spacedn(x, y, &bf);
  697.     while (*bf == ' ')
  698.         if (spacedn(x, y, &bf) == 0)
  699.             return;
  700.     while (*bf != ' ')    {
  701.         if (*x == 0)
  702.             return;
  703.         if (spacedn(x, y, &bf) == 0)
  704.             return;
  705.     }
  706.     spaceup(x, y, &bf);
  707. }
  708.  
  709. /* ------------------------------------------------------------------------ */
  710. static int spacedn(int *x, int *y, char **bf)
  711. {
  712.     if (*bf == topptr)
  713.         return 0;
  714.     --(*bf);
  715.     backspace(x, y);
  716.     return 1;
  717. }
  718.  
  719.  
  720. /* ------------------------------------------------------------------------ */
  721. /*               move the buffer offset forward one position                */
  722. /* ------------------------------------------------------------------------ */
  723. static void forward(int *x, int *y)
  724. {
  725.     int ww = wwd;
  726.  
  727.     (*x)++;
  728.     if (*x == ww)    {
  729.         downward(y);
  730.         *x = 0;
  731.     }
  732. }
  733.  
  734. /* ------------------------------------------------------------------------ */
  735. /*                move the buffer offset down one position                  */
  736. /* ------------------------------------------------------------------------ */
  737. static int downward(int *y)
  738. {
  739.     if (*y < wht - 1)    {
  740.         (*y)++;
  741.         return 1;
  742.     }
  743.     else if ((bfptr + wsz) < endptr)    {
  744.         bfptr += wwd;
  745.         scroll(wnd, UP);
  746.         disp_line(wht-1);
  747.         return 1;
  748.     }
  749.     return 0;
  750. }
  751.  
  752. /* ------------------------------------------------------------------------ */
  753. /*                 move the buffer offset up one position                   */
  754. /* ------------------------------------------------------------------------ */
  755. static void upward(int *y)
  756. {
  757.     if (*y)
  758.         --(*y);
  759.     else if ((topptr + wwd) <= bfptr)    {
  760.         bfptr -= wwd;
  761.         scroll(wnd, DOWN);
  762.         disp_line(0);
  763.     }
  764. }
  765.  
  766. /* ------------------------------------------------------------------------ */
  767. /*                     display all the lines in a window                    */
  768. /* ------------------------------------------------------------------------ */
  769. static void display_text()
  770. {
  771.     int y = 0;
  772.  
  773.     if (do_display_text)
  774.         while (y < wht)
  775.             disp_line(y++);
  776. }
  777.  
  778. /* ------------------------------------------------------------------------ */
  779. /*                           Display a line                                 */
  780. /* ------------------------------------------------------------------------ */
  781. static void disp_line(int y)
  782. {
  783.     int x = 0, atr = WFACE;
  784.  
  785.     if (blkbeg || blkend)
  786.         if (lineno(y) >= blkbeg-1)
  787.             if (lineno(y) <= blkend-1)
  788.                 atr = WACCENT;
  789.     while (x < wwd)    {
  790.         displ(wnd, x+1, y+1, *(bfptr+y * wwd+x), atr);
  791.         x++;
  792.     }
  793. }
  794.  
  795. /* ------------------------------------------------------------------------ */
  796. /*                       set insert/exchange cursor shape                   */
  797. /* ------------------------------------------------------------------------ */
  798. static void insert_line()
  799. {
  800.     vsetcurtype(inserting ? 0x0106 : 0x0607);
  801. }
  802.  
  803.